/*
 * $Id: sysapi_mkdir.c,v 1.2 2005-12-26 07:41:48 bacon Exp $ 
 */

#include <xp/bas/sysapi.h>
#include <xp/bas/locale.h>
#include <xp/bas/errno.h>

int xp_mkdir (const xp_char_t* path, xp_mode_t mode)
{
#ifdef _WIN32
	/* TODO: find a way to support atomic mkdir call */

	if (CreateDirectory(path, XP_NULL) == 0) {
		xp_seterrno (xp_maperrno(GetLastError()));
		return -1;
	}

	if (xp_chmod(path, mode) == -1) {
		RemoveDirectory (path);
		return -1;
	}

	return 0;
#else

#ifdef XP_CHAR_IS_MCHAR
	return mkdir (path, mode);
#else
	xp_mchar_t path_mb[XP_PATH_MAX + 1];
	if (xp_wcstomcs_strict(path, path_mb, xp_countof(path_mb)) == -1) {
		xp_seterrno (XP_EILSEQ);
		return -1;
	}
	return mkdir (path_mb, mode);
#endif

#endif
}

